import { reactive } from 'vue'
import { defineStore } from 'pinia'

export const useUserMsg = defineStore('counter', () => {
  // 获取本地存储中的用户状态，用于做状态管理器中的用户初始状态
  const uMsg = localStorage.getItem("userMsg") ? JSON.parse(localStorage.getItem("userMsg")) : {};

  // 设置初始状态
  const msg = reactive({
    token: uMsg.token || "",
    nickname: uMsg.nickname || "",
    mobile: uMsg.mobile || "",
    uid: uMsg.uid || "",
    avatar: uMsg.avatar || "",
    qf_state: uMsg.qf_state || ""
  })

  // 状态改变
  function changeMsg( { token, nickname, mobile, uid, avatar, qf_state } ) {
    if(token) msg.token = token;
    if(nickname) msg.nickname = nickname;
    if(mobile) msg.mobile = mobile;
    if(uid) msg.uid = uid;
    if(avatar) msg.avatar = avatar;
    if(qf_state) msg.qf_state = qf_state;
  }

  // 暴露状态和方法
  return { msg, changeMsg }
})
